import { defineStore } from "pinia";
import imgUrl from "../config/config";

export const useUserStore = defineStore("user", {
  state: () => ({
    userName: "",
    user: {},
  }),
  actions: {
    avatarUrl: (avatar) => {
      if (!avatar)
        return imgUrl.serverUrl + "/public/images/avatar/default.jpg";
      if (avatar.startsWith("http")) return avatar;
      return imgUrl.serverUrl + "/public/images" + avatar;
    },
    logout: () => {
      // 清空用户信息
      useUserStore().$reset();
      console.log("退出登录");
      window.location.href = "/login";
    },
  },
  persist: true,
});
